﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Reflection;
using NUnit.Framework;
using NHibernate.Audit.Cfg;
using NHibernate.Cfg;
using NHibernate.Mapping;

namespace NHibernate.Audit.Contract.Cfg
{

    /// <summary>
    /// Contract for NHibernate.Audit.Cfg.RevisionConfiguration
    /// </summary>
    /// <remarks>
    /// Inherit this test fixture to prove an RevisionConfiguration implementation
    /// </remarks>
    public abstract class RevisionConfigurationContract : AbstractContract<RevisionConfiguration>
    {

        protected override bool FixtureRequiresNHibernate()
        {
            return false;
        }

        protected override void Configure(NHibernate.Cfg.Configuration configuration)
        {
            // AddResource(configuration, Integration.Basic.BasicTestEntity1.MAPPING_RESOURCE_NAME);
        }

        protected override void OnFixtureSetUp()
        {
            basicTestEntity1 = AbstractFixture.mappings.GetMapping(MappingsCache.BASIC_ENTITY_1);
            base.OnFixtureSetUp();
        }

        private PersistentClass basicTestEntity1;

        [Test]
        public void revision_class_is_not_null()
        {
            var rc = ServiceLocator.GetInstance<RevisionConfiguration>();
            Assert.IsNotNull(rc.GetRevisionClass());
        }

        [Test]
        public void revision_class_is_a_class_or_interface()
        {
            var rc = ServiceLocator.GetInstance<RevisionConfiguration>();
            var actual = rc.GetRevisionClass();
            Assert.IsTrue(actual.IsClass || actual.IsInterface);
        }

        [Test]
        public void revision_identity_property_name_is_not_null_or_empty()
        {
            var rc = ServiceLocator.GetInstance<RevisionConfiguration>();
            string actual = rc.GetRevisionIdentityPropertyName();
            Assert.IsNotNullOrEmpty(actual);
        }

        [Test]
        public void revision_property_exists_on_revision_class()
        {
            var rc = ServiceLocator.GetInstance<RevisionConfiguration>();
            var revisionClass = rc.GetRevisionClass();
            string propertyName = rc.GetRevisionIdentityPropertyName();
            var property = revisionClass.GetProperty(propertyName);
            Assert.IsNotNull(property);
        }

        [Test]
        public void timestamp_property_name_is_not_null_or_empty()
        {
            var rc = ServiceLocator.GetInstance<RevisionConfiguration>();
            string actual = rc.GetRevisionTimestampPropertyName();
            Assert.IsNotNullOrEmpty(actual);
        }

        [Test]
        public void timestamp_property_exists_on_revision_class()
        {
            var rc = ServiceLocator.GetInstance<RevisionConfiguration>();
            var revisionClass = rc.GetRevisionClass();
            string propertyName = rc.GetRevisionTimestampPropertyName();
            var property = revisionClass.GetProperty(propertyName);
            Assert.IsNotNull(property);
        }

        [Test]
        public void revision_column_name_is_not_null_or_empty()
        {
            var rc = ServiceLocator.GetInstance<RevisionConfiguration>();
            var rootClass = basicTestEntity1;
            var tbl = rootClass.Table;
            var actual = rc.GetRevisionColumnName(tbl);
            Assert.IsNotNullOrEmpty(actual);
        }


        [Test]
        public void revision_property_name_is_not_null_or_empty()
        {
            var rc = ServiceLocator.GetInstance<RevisionConfiguration>();
            var rootClass = basicTestEntity1;
            var actual = rc.GetRevisionPropertyName(rootClass);
            Assert.IsNotNullOrEmpty(actual);
        }

        [Test]
        public void GetRevisionIdentityProperty_returns_a_property()
        {
            var rc = ServiceLocator.GetInstance<RevisionConfiguration>();
            var actual = rc.GetRevisionIdentityProperty();
            Assert.IsNotNull(actual);
        }

        [Test]
        public void GetRevisinIdentityPropertyType_returns_a_type()
        {
            var rc = ServiceLocator.GetInstance<RevisionConfiguration>();
            var actual = rc.GetRevisionIdentityPropertyType();
            Assert.IsNotNull(actual);
        }
        
    }
}
